package com.vincent.seeyou.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vincent.seeyou.R;
import com.vincent.seeyou.SYApplication;
import com.vincent.seeyou.util.CollectionUtils;
import com.vincent.seeyou.util.ImageLoadOptions;

import java.util.List;

import cn.bmob.im.BmobUserManager;
import cn.bmob.im.bean.BmobInvitation;
import cn.bmob.im.config.BmobConfig;
import cn.bmob.im.db.BmobDB;
import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.listener.UpdateListener;

public class NewFriendActivity extends BaseAndCheckInActivity {

    private Context mContext;
    private TextView textView_name;
    private ImageView imageView_avatar;

    private List<BmobInvitation> list;
    private BmobInvitation msg;
    int status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);
        mContext = this;
        list = BmobDB.create(this).queryBmobInviteList();
        initView();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_friend, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        msg = list.get(0);
        textView_name = (TextView) findViewById(R.id.textView_name);
        imageView_avatar = (ImageView) findViewById(R.id.imageView_avator);


        textView_name.setText(msg.getFromname());
        String avatar = msg.getAvatar();
        if (avatar != null && !avatar.equals("")) {
            ImageLoader.getInstance().displayImage(avatar, imageView_avatar, ImageLoadOptions.getOptions());
        } else {
            imageView_avatar.setImageResource(R.drawable.ic_drawer);
        }


        status = msg.getStatus();


    }

    public void onAgree(View view) {
        if (status == BmobConfig.INVITE_ADD_NO_VALIDATION) {
            BmobLog.i("点击同意按钮:" + msg.getFromid());
            agressAdd((Button)view, msg);
        }
        else if(status==BmobConfig.INVITE_ADD_AGREE) {

        }

    }

    private void agressAdd(final Button btn_add,final BmobInvitation msg){
        final ProgressDialog progress = new ProgressDialog(mContext);
        progress.setMessage("正在添加...");
        progress.setCanceledOnTouchOutside(false);
        progress.show();
        try {
            //同意添加好友
            BmobUserManager.getInstance(mContext).agreeAddContact(msg, new UpdateListener() {

                @Override
                public void onSuccess() {
                    // TODO Auto-generated method stub
                    progress.dismiss();
                    btn_add.setText("已同意");

                    btn_add.setEnabled(false);
                    //保存到application中方便比较
                    SYApplication.getInstance().setContactList(CollectionUtils.list2map(BmobDB.create(mContext).getContactList()));
                }

                @Override
                public void onFailure(int arg0, final String arg1) {
                    // TODO Auto-generated method stub
                    progress.dismiss();
                    ShowToast("添加失败: " +arg1);
                }
            });
        } catch (final Exception e) {
            progress.dismiss();
            ShowToast("添加失败: " +e.getMessage());
        }
    }
}
